package carol.pvp.producer.impl;

import java.util.ArrayList;
import java.util.Iterator;

import wangy.pvp.basiclibrary.ClassAnalysisResult;
import wangy.pvp.basiclibrary.basicType.AssClass;
import wangy.pvp.basiclibrary.basicType.AssMethod;
import wangy.pvp.basiclibrary.basicType.PostAssertion;

import carol.pvp.producer.LanguageProducer;
import carol.pvp.producer.basicType.AssCode;

public abstract class LanguageProducerImpl implements LanguageProducer{
	ArrayList<AssCode> codes = new ArrayList<AssCode>();
	
	public ArrayList<AssCode> getCodes(ClassAnalysisResult result){
		produceCodes(result);
		return codes;
	}
	
	protected void produceCodes(ClassAnalysisResult result){
		Iterator<AssClass> classes = result.getSimpleClass();
		
		while (classes.hasNext()){
			AssClass assClass = classes.next();
			Iterator<AssMethod> methods = result.getClassMethod(assClass);
			
			while (methods.hasNext()){
				AssMethod assMethod = methods.next();
				AssCode assCode = new AssCode();
				
				assCode.setPackageName(assClass.getPackageName());
				assCode.setClassName(assClass.getClassName());
				assCode.setPreLine(assMethod.getStartLine());
				while(assMethod.getEndLines().hasNext()){
					assCode.addPostLine(assMethod.getEndLines().next());
				}
				
				Iterator<PostAssertion> postAss = assMethod.getPostAss();			
				assCode.setPreCodes(producePreCode(postAss));
				assCode.setPostCodes(producePostCode(postAss));

				codes.add(assCode);
			}
		}
	}
	
	protected abstract ArrayList<String> producePreCode(Iterator<PostAssertion> postAss);
	protected abstract ArrayList<String> producePostCode(Iterator<PostAssertion> postAss);
}
